package org.itboys.mobile.service.mongo.weixin.meal;

import org.itboys.mdl.conmon.Finals;
import org.itboys.mdl.dto.meal.MdlMealDto;
import org.itboys.mdl.entity.mongo.meal.MdlMeal;
import org.itboys.mdl.entity.mongo.meal.MdlMealProduct;
import org.itboys.mobile.service.mongo.web.meal.MdlMealProductService;
import org.itboys.mongodb.core.MongoDataSource;
import org.itboys.mongodb.service.BaseMongoService;
import org.itboys.param.ParamUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 作者：zzq
 * 时间：2017/12/3
 * 描述：迈迪龙_微信_套餐
 */
@Service
public class MdlWeixinMealService extends BaseMongoService<MdlMeal>{
    @Resource(name = "mdlDs")
    private MongoDataSource mdlDataSource;
    @Autowired
    private MdlMealProductService mealProductService;
    @Override
    protected MongoDataSource getMongoDataSource() {
        return mdlDataSource;
    }

    @Override
    protected Class<MdlMeal> getEntityClass() {
        return MdlMeal.class;
    }

    /**
     * 查询
     * @param request
     * @return
     */
    public List<MdlMealDto> selectMeal(HttpServletRequest request){
        Map<String,Object> param= ParamUtil.packageMongoExactParam(request);
        param.put("isDeleted", Finals.NO);
        return packageDtoMuilt(super.list(param));
    }

    /**
     * 组装多个
     * @param meals
     * @return
     */
    public List<MdlMealDto> packageDtoMuilt(List<MdlMeal> meals){
        List<MdlMealDto> dtos=new ArrayList<MdlMealDto>();
        List<Long> mealIds=new ArrayList<Long>();
        for(MdlMeal meal:meals){
            mealIds.add(meal.getId());
        }
        Map<String,Object> param = new HashMap<String,Object>();
        param.put("mealId in",mealIds);
        param.put("isDeleted", Finals.NO);
        List<MdlMealProduct> allProducts=mealProductService.list(param);
        for(MdlMeal meal:meals){
            List<MdlMealProduct> products=new ArrayList<MdlMealProduct>();
            for(MdlMealProduct mealProduct:allProducts){
                if(meal.getId()==mealProduct.getMealId().longValue()){
                    products.add(mealProduct);
                }
            }
            MdlMealDto dto=packageDtoSingle(meal);
            dto.setProducts(products);
        }
        return dtos;
    }

    /**
     * 组装单个
     * @param meal
     * @return
     */
    public MdlMealDto packageDtoSingle(MdlMeal meal){
        MdlMealDto dto=new MdlMealDto(meal.getId(),meal.getCreateTime(),meal.getCreateDate(),meal.getName(),meal.getViceName(),meal.getMainId(),
                meal.getMainName(),meal.getHouseId(),meal.getHouseStandard());
        return dto;
    }
}
